package tools.pfam;

import java.io.BufferedReader;
import java.io.FileReader;

public class pfamParser {

		private BufferedReader in;
		private pfamAlignment current,next;
		private boolean more;
		private String s;
		
		public pfamParser(String inFile)throws Exception{
			in=new BufferedReader(new FileReader(inFile));
			more=false;
			readNext();
		}
		public boolean hasMore(){
			return more;
		}
		public pfamAlignment next()throws Exception{
			if(hasMore()){
				current=new pfamAlignment(next);
				readNext();
			}else{
				throw new Exception("No more too read...");
			}
			return current;
		}
		
		private void readNext()throws Exception{
			s=in.readLine();
			if(s==null){
				more=false;
			}else{
				try{
					String[] l=s.split("\\s+");
//					if(l.length<10){
//						l=s.split("\t");
//					}
					if(l.length>=10){
						next= new pfamAlignment();
						next.setQname(l[0]);
						next.setQstart(Integer.parseInt(l[1]));
						next.setQend(Integer.parseInt(l[2]));
						next.setPfamID(l[3]);
						next.setHmmstart(Integer.parseInt(l[4]));
						next.setHmmend(Integer.parseInt(l[5]));
						next.setAlignmentType(l[6]);
						next.setScore(Double.parseDouble(l[7]));
						next.setEvalue(Double.parseDouble(l[8]));
						next.setHmmname(l[9]);
						more=true;
					}else{
						readNext();
					}
				}catch(Exception e){
					System.err.println("Corrupt line: "+s);
					e.printStackTrace(System.err);
					more=false;
				}
			}
		}
	
}
